package com.nowcoder.community.config;/**
 * @description:
 * @Author HP
 * @create 2022/1/3 20:41
 */

import com.nowcoder.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * @author HP
 * @create 2022-01-03 20:41
 * @desc
 **/
@Configuration
public class QuartzConfig {

    //刷新帖子分数任务
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail(){  //FactoryBean与spring IOC顶层的BeanFactory不同
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true); //哪怕将来这个任务不再运行了，甚至它的Trigger都没有了，也要留在数据库（不删）
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    // 配置Trigger（可用SimpleTriggerFactoryBean或复杂的ConTriggerFactoryBean
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail){  //JobDetail参数alphaJobDetail要与上面定义alphaJobDetail()的名字一致，以保证有多个实例的时候，优先选择名字一致的
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000*60*5); //每5分钟执行一遍
        factoryBean.setJobDataMap(new JobDataMap()); //用来存储job的状态
        return factoryBean;
    }
}
